package com.cws.project.cws.clazz.controller;

import java.util.List;

import com.cws.project.system.user.domain.User;
import com.cws.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.project.cws.clazz.domain.Clazz;
import com.cws.project.cws.clazz.service.IClazzService;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.web.page.TableDataInfo;

/**
 * 班级信息Controller
 *
 * @author xyl
 * @date 2021-12-20
 */
@Controller
@RequestMapping("/cws/clazz")
public class ClazzController extends BaseController {
    private String prefix = "cws/clazz" ;

    @Autowired
    private IClazzService clazzService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("cws:clazz:view")
    @GetMapping()
    public String clazz() {
        return prefix + "/clazz" ;
    }

    /**
     * 查询班级信息列表
     */
    @RequiresPermissions("cws:clazz:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Clazz clazz) {
        startPage();
        //教师查看自己班级的学生
        User sysUser = getSysUser();
        if(sysUser.isTeacher() || sysUser.isInstructor()){
            clazz.setUserId(sysUser.getUserId());
        }
        List<Clazz> list = clazzService.selectClazzList(clazz);
        return getDataTable(list);
    }

    /**
     * 导出班级信息列表
     */
    @RequiresPermissions("cws:clazz:export")
    @Log(title = "班级信息" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Clazz clazz) {
        startPage();
        //教师查看自己班级的学生
        User sysUser = getSysUser();
        if(sysUser.isTeacher() || sysUser.isInstructor()){
            clazz.setUserId(sysUser.getUserId());
        }
        List<Clazz> list = clazzService.selectClazzList(clazz);
        ExcelUtil<Clazz> util = new ExcelUtil<Clazz>(Clazz.class);
        return util.exportExcel(list, "clazz");
    }

    /**
     * 新增班级信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<User> instructors = userService.getUserListByRoleId(2);
        mmap.put("instructors" , instructors);
        return prefix + "/add" ;
    }

    /**
     * 新增保存班级信息
     */
    @RequiresPermissions("cws:clazz:add")
    @Log(title = "班级信息" , businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Clazz clazz) {
        clazz.setCreateBy(getLoginName());
        return toAjax(clazzService.insertClazz(clazz));
    }

    /**
     * 修改班级信息
     */
    @GetMapping("/edit/{clazzId}")
    public String edit(@PathVariable("clazzId") Long clazzId, ModelMap mmap) {
        Clazz clazz = clazzService.selectClazzById(clazzId);
        mmap.put("clazz" , clazz);
        List<User> instructors = userService.getUserListByRoleId(2);
        mmap.put("instructors" , instructors);
        return prefix + "/edit" ;
    }

    /**
     * 修改保存班级信息
     */
    @RequiresPermissions("cws:clazz:edit")
    @Log(title = "班级信息" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Clazz clazz) {
        return toAjax(clazzService.updateClazz(clazz));
    }

    /**
     * 删除班级信息
     */
    @RequiresPermissions("cws:clazz:remove")
    @Log(title = "班级信息" , businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(clazzService.deleteClazzByIds(ids));
    }
}
